package com.xggstudio.crm.ui.mvp.v;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.xggstudio.crm.R;
import com.xggstudio.crm.base.BaseFragment;
import com.xggstudio.crm.base.Constant;
import com.xggstudio.crm.tools.EndlessRecyclerOnScrollListener;
import com.xggstudio.crm.ui.mvp.m.CusListBean;
import com.xggstudio.crm.ui.mvp.p.P_CusList;
import com.xggstudio.crm.ui.mvp.v.adapter.BaseCommonAdapter;
import com.xggstudio.crm.ui.mvp.v.adapter.CommonAdapter;
import com.xggstudio.crm.ui.mvp.v.adapter.ViewHolder;
import com.xggstudio.crm.ui.mvp.v.pop.CusAssistPopWindow;
import com.xggstudio.crm.ui.mvp.v.pop.CusComePopWindow;
import com.xggstudio.crm.ui.mvp.v.pop.CusTypePopWindow;
import com.xggstudio.crm.ui.weiget.screen.ScreenLayout;

import java.util.ArrayList;
import java.util.List;

public class CustomerFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener,ICusList {

    private SwipeRefreshLayout swipeRefLayout;

    private TabLayout tabLayout;

    private RecyclerView list;

    private BaseCommonAdapter adapter;

    private int page = 1;

    private int count = 10;

    private P_CusList pCusList;

    CusTypePopWindow cusTypePopWindow;

    CusComePopWindow cusComePopWindow;

    CusAssistPopWindow cusAssistPopWindow;

    private boolean isLoad = false;

    @Override
    protected int getLayout() {
        return R.layout.fragment_customer;
    }

    @Override
    protected void init(View v) {

        swipeRefLayout = (SwipeRefreshLayout) $(R.id.swipefresh);
        swipeRefLayout.setOnRefreshListener(this);

        tabLayout = (TabLayout) $(R.id.tablayout);

        list = (RecyclerView) $(R.id.list);

        initList();
        intiTabLayout();

        pCusList = new P_CusList(this);

        pCusList.getData(page,count);
    }

    @Override
    public void onRefresh() {
        swipeRefLayout.setRefreshing(false);
    }

    private void initList(){

        List<CusListBean.ResultBean> data = new ArrayList<>();


        adapter = new BaseCommonAdapter<CusListBean.ResultBean>(getActivity(),R.layout.view_customer,data) {
            @Override
            public void convert(ViewHolder holder, final CusListBean.ResultBean bean, int position) {

                holder.setText(R.id.name,bean.getClient_name());
                holder.setText(R.id.phone,bean.getClient_phone());
                holder.setText(R.id.functionary,bean.getCreater());
                holder.setText(R.id.content,bean.getContents());

                holder.setOnClickListener(R.id.layout, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        Bundle b = new Bundle();
                        b.putInt(Constant.CUS_ID,bean.getId());

                        startToActivity(AddEditCusActivity.class,b);
                    }
                });

            }
        };

        list.setLayoutManager(new LinearLayoutManager(getActivity()));
        list.addOnScrollListener(onScrollListener);
        list.setAdapter(adapter);
    }

    private void intiTabLayout(){

        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {

              switch (tab.getPosition()){
                    case 0:
                        showPopType();
                        break;
                    case 1:
                        showPopCome();
                        break;
                    case 2:

                        break;
                    case 3:

                        break;
                }

                Log.e("CustomerFragment", "onTabSelected: ");

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                Log.e("CustomerFragment", "onTabUnselected: ");
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                Log.e("CustomerFragment", "onTabReselected: ");
                switch (tab.getPosition()){
                    case 0:
                        showPopType();
                        break;
                    case 1:
                        showPopCome();
                        break;
                    case 2:
                        showAssistCome();
                        break;
                    case 3:

                        break;
                }
            }
        });

    }


    private void showPopType(){

        if(cusTypePopWindow == null) {
            cusTypePopWindow = new CusTypePopWindow(getActivity(), $(R.id.tab_type));
            cusTypePopWindow.setFocusable(true);
            cusTypePopWindow.showAsDropDown(tabLayout,0,0);
        }else{
            if(cusTypePopWindow.isShowing()){
                cusTypePopWindow.dismiss();
            }else{
               // cusTypePopWindow.dismiss();
                cusTypePopWindow.showAsDropDown(tabLayout,0,0);
            }
        }
    }

    private void showPopCome(){

        if(cusComePopWindow == null) {
            cusComePopWindow = new CusComePopWindow(getActivity(), $(R.id.tab_type));
            cusComePopWindow.setFocusable(true);
            cusComePopWindow.showAsDropDown(tabLayout,0,0);
        }else{
            if(cusComePopWindow.isShowing()){
                cusComePopWindow.dismiss();
            }else{
                cusComePopWindow.showAsDropDown(tabLayout,0,0);
            }
        }
    }

    private void showAssistCome(){

        if(cusAssistPopWindow == null) {
            cusAssistPopWindow = new CusAssistPopWindow(getActivity(), $(R.id.tab_type));
            cusAssistPopWindow.setFocusable(true);
            cusAssistPopWindow.showAsDropDown(tabLayout,0,0);
        }else{
            if(cusAssistPopWindow.isShowing()){
                cusAssistPopWindow.dismiss();
            }else{
                cusAssistPopWindow.showAsDropDown(tabLayout,0,0);
            }
        }
    }

    @Override
    public void showData(CusListBean data) {

        if(data.getResult().size() == 0){
            return;
        }
        adapter.add(data.getResult());

        isLoad = true;
        page ++;
    }

    @Override
    public void showError(String msg) {

    }

    @Override
    public void noNet() {

    }

    public EndlessRecyclerOnScrollListener  onScrollListener = new EndlessRecyclerOnScrollListener() {
        @Override
        public void onLoadMore(View view) {
            if(isLoad) {
                pCusList.getData(page, count);
                isLoad = false;
            }
        }

        @Override
        public void onCancelMore() {

        }

        @Override
        public void onScrolledUp() {

        }

        @Override
        public void onScrolledDown() {

        }
    };
}
